//
//  MLLoginTextField.m
//  Molian
//
//  Created by 胡津 on 15/4/15.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "MLLoginTextField.h"

@implementation MLLoginTextField
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _line = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 0.25)];
        _line.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
        [self addSubview:_line];
        self.toBeNumber = 100;

        if (IS_IOS8) {
            self.delegate = self;
        }
        
    }
    return self;
    
}

-(void)style
{
    if (self.leftPrompt) {
        UILabel *view = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [Utils LabeSizeText:self.leftPrompt Font:[UIFont fontWithName:FontName size:18]].width, 40)];
        view.textAlignment = NSTextAlignmentLeft;
        view.font = [UIFont fontWithName:FontName size:18];
        view.text = self.leftPrompt;
        view.textColor = [UIColor blackColor];
        self.leftView = view;//缩进
        self.leftViewMode  = UITextFieldViewModeAlways;
    }
    
    if (self.leftImage) {
        UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        [view setContentMode:UIViewContentModeScaleAspectFit];
        view.image = self.leftImage;
        self.leftView = view;//缩进
        self.leftViewMode  = UITextFieldViewModeAlways;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    //string就是此时输入的那个字符 textField就是此时正在输入的那个输入框 返回YES就是可以改变输入框的值 NO相反
    if ([string isEqualToString:@"\n"])  //按会车可以改变
    {
        [self resignFirstResponder];
        return NO;
    }
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (self == textField)  //判断是否时我们想要限定的那个输入框
    {
        if ([toBeString length] > self.toBeNumber) { //如果输入框内容大于20则弹出警告
            return NO;
        }
    }
    return YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
